package cn.org.cute.ebocy.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *  处理数据压缩的类方法,会包括zip等一些压缩方法的处理
 * @Copyright©2011, 2011 EBO CHANGYOU Co.td. All rights reserved.
 * @Version 1.0.0
 * @since jdk1.6
 * @author xiaolei
 * @date 2011-12-6
 */
public class DataCompression {
	
	/**
	 * 用zip方式压缩数据 
	 * @param zipData 压缩前的原始数据
	 * @return
	 */
    public static  byte[] doZip(Object zipData) {  
    	//System.out.println("压缩前size="+(ByteToObjectUtil.objectToByte(zipData)).length);
        if (zipData == null)  return null;  
        //long startTime = System.currentTimeMillis();
        byte[] compressed;  
        ByteArrayOutputStream out = null;  
        ZipOutputStream zout = null;  
  
        try {  
            out = new ByteArrayOutputStream();  
            zout = new ZipOutputStream(out);  
            zout.putNextEntry(new ZipEntry("0"));  
             
            byte[] bytes = ByteToObjectUtil.objectToByte(zipData);  
            zout.write(bytes);  
            zout.closeEntry();  
            compressed = out.toByteArray();  
        } catch (IOException e) {  
            e.printStackTrace();  
            compressed = null;  
        } finally {  
            if (zout != null) {  
                try {  
                    zout.close();  
                } catch (IOException e) {  
                }  
            }  
            if (out != null) {  
                try {  
                    out.close();  
                } catch (IOException e) {  
                }  
            }  
        } 
        //long endTime = System.currentTimeMillis();
       // System.out.println("消耗时间="+(endTime-startTime));
       // System.out.println("压缩后size="+compressed.length);
        return compressed;  
    } 
    

	
	
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Object zipData = "{\"name\":\"肖磊\",\"sex\":1,\"pic\":\"http://www.baidu.com\"},{\"name\":\"肖磊\",\"sex\":1,\"pic\":\"http://www.baidu.com\"},{\"name\":\"肖磊\",\"sex\":1,\"pic\":\"http://www.baidu.com\"},{\"name\":\"肖磊\",\"sex\":1,\"pic\":\"http://www.baidu.com\"}{\"name\":\"肖磊\",\"sex\":1,\"pic\":\"http://www.baidu.com\"}";
		System.out.println(DateDecompress.doUnZip(doZip(zipData)));

	}

}
